package se.sll.itintegration.catalogue.gui.service.view.capacity;

import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.SecurityContext;

import lombok.extern.slf4j.Slf4j;

import org.apache.camel.ExchangePattern;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.cdi.CdiCamelContext;

import org.apache.shiro.SecurityUtils;
import se.sll.itintegration.catalogue.gui.camel.cdi.CamelRoute;
import se.sll.service.catalogue.common.exception.ResourceNotFoundException;
import se.sll.service.catalogue.common.exception.ServiceCatalogueException;
import se.sll.service.catalogue.common.exception.ServiceException;
import se.sll.service.catalogue.common.exception.ValidationException;

/**
 *
 * @author Christer Palm, Nogui
 */
@Path("/view/capacity")
@Slf4j
@Stateless
public class CapacityViewService {
    @Inject
    private CdiCamelContext camelCtx;

    @GET
    @Produces("application/json; charset=UTF-8")
    @Path("/")
    public CapacityView getCapacityView(@Context SecurityContext sc) throws ValidationException, ResourceNotFoundException, ServiceCatalogueException {
        log.trace("getCapacityView() ENTER");
        ProducerTemplate template = camelCtx.createProducerTemplate();
        CapacityView view = (CapacityView) template.sendBodyAndProperty(CamelRoute.GET_CAPACITY_VIEW, ExchangePattern.InOut, null, "sc", sc);
        log.trace("getCapacityView() EXIT");
        return view;
    }

    @PUT
    @Produces("application/json; charset=UTF-8")
    @Path("/")
    public List<CapacityViewUnit> updateCapacityView(@Context SecurityContext sc, CapacityView capacityView) throws ServiceException {
        log.trace("updateCapacityView() ENTER");
        SecurityUtils.getSubject().getSession().touch();
        ProducerTemplate template = camelCtx.createProducerTemplate();
        @SuppressWarnings("unchecked")
        List<CapacityViewUnit> response = (List<CapacityViewUnit>) template.sendBodyAndProperty(CamelRoute.UPDATE_CAPACITY_VIEW, ExchangePattern.InOut, capacityView, "sc", sc);
        log.trace("updateCapacityView() EXIT");
        return response;
    }
}
